﻿#include "memo.h"
#include "ui_memo.h"

Memo::Memo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Memo)
{
    ui->setupUi(this);
}

Memo::~Memo()
{
    delete ui;
}

void Memo::setUserLand(const QString &location)
{
    userLand=location;
}

void Memo::load()
{
    QFile file(userLand + e_memo);
    if(file.exists())
    {
        file.open(QFile::ReadWrite | QFile::Text);
        QTextStream text(&file);
        QString content=text.readAll();
        ui->content->setPlainText(content);
        file.close();
    }
}

void Memo::detect()
{
    QFile file(userLand + e_memo);
    if(file.exists())
    {
        file.open(QFile::ReadWrite | QFile::Text);
        QTextStream text(&file);
        QString content=text.readAll();
        file.close();
        if(!content.isEmpty())
        {
            ui->content->setPlainText(content);
            this->show();
        }
    }
}

void Memo::save()
{
    QFile file(userLand + e_memo);
    file.open(QFile::WriteOnly | QFile::Text);
    QTextStream text(&file);
    text << ui->content->toPlainText();
    file.close();
}

void Memo::closeEvent(QCloseEvent *)
{
    save();
}
